const dbPool = require("../database")


exports.addBanner = function (req, res) {
  if (!req.file || req.file.fieldname !== "banner") return res.cc("图片不能为空！");
  const filePath = "/assets/" + req.file.filename;
  const sqlStr = "insert into banner set ?";
  dbPool.query(sqlStr, { image: filePath }, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) return res.cc("添加图片失败！");
    res.cc("添加图片成功！", 1);
  })
}

exports.getBanner = function (req, res) {
  const sqlStr = "select * from banner order by id desc limit 0, 5"
  dbPool.query(sqlStr, (err, results) => {
    if (err) return res.cc(err);
    res.send({
      status: 1,
      message: "获取图片成功！",
      data: results,
    })
  })
}